import HeadPrefab from "../../component/HeadPrefab";
import { EventManager } from "../../engine/EventManager";
import { Logger } from "../../engine/Logger";
import UIManager from "../../engine/UIManager";
import ViewBase from "../../engine/ViewBase";
import { SocketClient } from "../../net/SocketClient";
import { EventConfig } from "../config/EventConfig";
import { ViewConfig } from "../config/ViewConfig";
import { UserModel } from "../model/UserModel";
import { UIHelper } from "../util/UIHelper";
import { ViewCheck } from "./ViewCheck";


const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewTuiguang extends ViewBase {

    private txtSearchId: cc.EditBox
    private defaultItemNode: cc.Node;
    private dataList: Array<object> = []

    private page: number = 1;
    private pageSize: number = 5;
    private totalPage: number = 1

    onEnable(): void {
        super.onEnable()
        EventManager.instance.on(EventConfig.Event_Update_UserInfo, () => {
            this.getInfo()
        }, this)
        let btnPre: cc.Node = this.findChild(this.node, "btnPre")
        UIHelper.onAddEvent(btnPre, () => {
            if (this.page > 1) {
                this.page--
                this.refresh(this.dataList)
            }
        })
        let btnNext: cc.Node = this.findChild(this.node, "btnNext")
        UIHelper.onAddEvent(btnNext, () => {
            if (this.page < this.totalPage) {
                this.page++
                this.refresh(this.dataList)
            }
        })
        this.defaultItemNode = this.findChild(this.node, "meberScrollView/view/content/defaultItemNode")
        this.txtSearchId = this.getChild(this.node, "txtSearchId", cc.EditBox)

        let btnSearch: cc.Node = this.findChild(this.node, "btnSearch")
        UIHelper.onAddEvent(btnSearch, () => {
            if (this.txtSearchId.string == "") {
                this.refresh(this.dataList)
            } else {
                this.searchId(parseInt(this.txtSearchId.string))
            }
        })
        this.refresh()
        this.getInfo()
    }

    private getInfo() {
        let searchId: number = parseInt(this.txtSearchId.string)
        if (isNaN(searchId)) {
            searchId = 0
        }
        this.page = 1
        SocketClient.req_invite_list(searchId, this.page, this.pageSize, (data: Array<object>) => {
            this.dataList = data
            this.refresh(data)
        })
    }

    private searchId(id: number) {
        let resultList: Array<object> = []
        for (let i = 0; i < this.dataList.length; i++) {
            if (this.dataList[i]["userid"] == id) {
                resultList.push(this.dataList[i])
            }
        }
        this.refresh(resultList)
    }

    private deleteId(id: number) {
        for (let i = 0; i < this.dataList.length; i++) {
            if (this.dataList[i]["userid"] == id) {
                this.dataList.splice(i, 1)
                break
            }
        }
        this.refresh(this.dataList)
    }

    private refresh(dataList: Array<object> = []) {
        let txtPage: cc.Label = this.getChild(this.node, "txtPage", cc.Label)
        this.totalPage = dataList.length % this.pageSize == 0 ? dataList.length / this.pageSize : Math.floor(dataList.length / this.pageSize) + 1;
        if (this.totalPage <= 0) {
            this.totalPage = 1;
        }
        txtPage.string = this.page + "/" + this.totalPage

        this.unscheduleAllCallbacks()
        this.hideAllChildren(this.defaultItemNode.parent)
        // for (let i = 0; i < dataList.length; i++) {
        for (let i = 0; i < this.pageSize; i++) {
            let realIndex: number = (this.page - 1) * this.pageSize + i
            let dataObj: object = dataList[realIndex];
            if (dataObj) {
                let item: cc.Node = this.defaultItemNode.parent.children[i]
                if (!item) {
                    this.scheduleOnce(() => {
                        this.initOne(dataObj, i)
                    }, 0.02 * i)
                } else {
                    item.active = true;
                    this.initOne(dataObj, i)
                }
            }
        }
    }

    private initOne(data: object, i: number) {
        let itemNode: cc.Node = this.defaultItemNode.parent.children[i]
        if (!itemNode) {
            itemNode = cc.instantiate(this.defaultItemNode)
            itemNode.parent = this.defaultItemNode.parent;
        }
        itemNode.active = true;
        let head: HeadPrefab = this.getChild(itemNode, "head", HeadPrefab)
        head.setHead(data["headimg"], data["lv"])
        let txtName: cc.Label = this.getChild(itemNode, "txtName", cc.Label)
        txtName.string = data["nick_name"]
        let txtId: cc.Label = this.getChild(itemNode, "txtId", cc.Label)
        txtId.string = "ID:" + data["userid"]
        let txtZuanshi: cc.Label = this.getChild(itemNode, "txtZuanshi", cc.Label)
        txtZuanshi.string = "" + UIHelper.formatCoin(data["gems"])
        let txtJindou: cc.Label = this.getChild(itemNode, "txtJindou", cc.Label)
        txtJindou.string = "" + UIHelper.formatCoin(data["coins"])
        let btnUnbind: cc.Node = this.findChild(itemNode, "btnUnbind")
        UIHelper.onAddEvent(btnUnbind, () => {
            UIHelper.showCheck("解除后将不再获得此玩家产生的收益，确定解除吗?", (isSuc: boolean) => {
                if (isSuc) {
                    SocketClient.req_unbind_invite(data["userid"], () => {
                        this.deleteId(data["userid"])
                    })
                }
            }, ViewCheck.Dialog_Type.All)
        })
        let btnGive: cc.Node = this.findChild(itemNode, "btnGive")
        UIHelper.onAddEvent(btnGive, () => {
            UIManager.instance.showView(ViewConfig.viewConfig.ViewGiveGem, null, data)
        })
        let btnGiveHistory: cc.Node = this.findChild(itemNode, "btnGiveHistory")
        UIHelper.onAddEvent(btnGiveHistory, () => {
            UIManager.instance.showView(ViewConfig.viewConfig.ViewGiveGemHistory, null, data)
        })
    }

}
